﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CuaHangBanGiay.Models;
using CuaHangBanGiay.Controllers;
using System.Web;
using CuaHangBanGiay.Controllers.ModelController;
using System.Web.Hosting;
using CuaHangBanGiay._Library;
using System.Data.Entity;
namespace WindowsFormsApplication1
{
   
    public partial class Form1 : Form
    {
        private CuaHangBanGiayDBContext db = new CuaHangBanGiayDBContext();
        public Form1()
        {
            InitializeComponent();
            listview();
            combobox_load();
            this.Show();
            
        }
        private void listview()
        {
            /*LISTVIEW*/
            this.listView1.Clear();
            //đặt chế độ hiển thị
            //chuyển sang chế độ column
            this.listView1.View = View.Details;
            //thêm column rỗng
            this.listView1.Columns.Add("");
            //thêm column ID

            this.listView1.Columns.Add("Kích thước");
            this.listView1.Columns.Add("Màu sắc");
            this.listView1.Columns[0].Width = 0;
           
        }
        private void combobox_load()
        {
            comboNhom.Items.Clear();
            NhomSanPhamController ctrl=new NhomSanPhamController(db);
            List<NhomSanPham2> list = ctrl.timkiem("", "", "", "");
            int i = 0;
            ComboboxItem item = new ComboboxItem();
            item.Text = "Chọn Nhóm";
            item.Value=0;  
            comboNhom.Items.Add(item);
            foreach (NhomSanPham2 sp in list)
            {
                ComboboxItem itm = new ComboboxItem();
                itm.Value = sp.id;
                itm.Text = sp.ten;
                this.comboNhom.Items.Add(itm);
                i++; 
            }
            comboNhom.SelectedIndex = 0;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(Application.StartupPath))));
           // MessageBox.Show(Environment.GetFolderPath(Environment.SpecialFolder.));
            //MessageBox.Show(Assemb));
      //      string webProjectFile = "/web.config";
            //string absolutePath = System.Web.HttpContext.Current.Server.MapPath(webProjectFile);
           // MessageBox.Show(System.AppDomain.CurrentDomain.BaseDirectory);
           // NhomSanPhamController a = new NhomSanPhamController();
          //  MessageBox.Show(a.s);
          //  CuaHangBanGiayDBContext db = new CuaHangBanGiayDBContext();
            SanPham sp = new SanPham();
            sp.masp = txtMasp.Text;
            sp.ten = txtTen.Text;
            sp.mota = txtMota.Text;
            NhomSanPham nhom=new NhomSanPhamController(db).get_by_id(TextLibrary.ToInt((comboNhom.SelectedItem as ComboboxItem).Value.ToString()));
            sp.nhomsanpham = nhom;
            sp.gia = TextLibrary.ToInt(txtGia.Text);
            HangSX hsx = new HangSX();
            hsx.tenhsx = txtHangsx.Text;
            sp.hangsx = hsx;
            foreach(ListViewItem item in listView1.Items)
            {
                KichThuoc kt = new KichThuoc();
                kt.giatri = item.SubItems[1].Text;
                MauSac mau = new MauSac();
                mau.giatri = item.SubItems[2].Text;
                ChiTietSP spt = new ChiTietSP();
                spt.kichthuoc = kt;
                spt.mausac = mau;
                spt.soluong = 0;
                sp.ds_sanpham_tag.Add(spt);
            }
           for(int i=0;i<comboHinh.Items.Count;i++)
            {
                ComboboxItem itm =comboHinh.Items[i] as ComboboxItem;
                //copy hinh vao thu muc images
                FileInfo fi = new FileInfo(itm.Value.ToString());
                string des = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(Application.StartupPath))) + @"\CuaHangBanGiay\Content\images\product\" + itm.Text;
                File.Delete(des);
                fi.CopyTo(des);
               //luu ten hinh vào db
                HinhAnh hinh = new HinhAnh();
                hinh.duongdan = itm.Text.ToString();
                sp.ds_hinhanh.Add(hinh);
            }
            SanPhamController ctrlsp = new SanPhamController(db);
            ctrlsp.add(sp);
            MessageBox.Show("OK");
        }

        private void btnSize_Click(object sender, EventArgs e)
        {
            //listView1.Items.Clear();
            if (!txtSize.Text.Equals("")&&!txtMau.Text.Equals(""))
            {
                ListViewItem itm = new ListViewItem();
                MessageBox.Show(txtMau.Text + " - " + txtSize.Text);
                itm.SubItems.Add(txtSize.Text);
                itm.SubItems.Add(txtMau.Text);
                this.listView1.Items.Add(itm);
            }
            txtSize.Text = "";
            txtMau.Text = "";
        }

        private void btnMau_Click(object sender, EventArgs e)
        {
           
        }

        private void btnHinh_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "file hinh|*.jpg|all file|*.*";
                dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
                dlg.Multiselect = true;
                comboHinh.Items.Clear();     
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    string[] tmp = dlg.FileNames;
                    BindingSource bs = new BindingSource();
                    dataGridView1.AutoGenerateColumns = false;
                    List<HinhAnhkim> data = new List<HinhAnhkim>();
                    List<Image> img=new List<Image>();
                    int j = 0;
                    foreach (string i in tmp)
                    {
                      //  FileInfo fi = new FileInfo(i);
                      //  MessageBox.Show(i);
                        HinhAnhkim a = new HinhAnhkim();
                      
                        ComboboxItem itm = new ComboboxItem();
                        itm.Value = i;
                        //lấy tên hình ảnh
                        string[] xxx = i.Split('\\');
                        itm.Text = xxx[xxx.Length - 1];
                        a.hinh_name = itm.Text;
                        a.hinh= Image.FromFile(i);
                    //    dataGridView1.Rows[j] = a;
                        a.check = true;
                        data.Add(a);
                        img.Add(a.hinh);
                        comboHinh.Items.Add(itm);
                        j++;
                        //lấy đường dẫn thư mục cần lưu
                      //  string des = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(Application.StartupPath)))+@"\CuaHangBanGiay\Content\images\product\"+ xxx[xxx.Length - 1];;
                       // MessageBox.Show(des);
                        //  File.Delete(des);
                        //over.
                       //fi.CopyTo(des);
                    }
                    comboHinh.SelectedIndex = 0;
                    bs.DataSource = data;
                 //   dataGridView1.DataSource = null;
                 //   dataGridView1.DataSource = bs;
                    dataGridView1.DataSource = data;
                    MessageBox.Show("thang cong ");
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
           
        }

        private void comboHinh_SelectedIndexChanged(object sender, EventArgs e)
        {

            Image img = Image.FromFile((comboHinh.SelectedItem as ComboboxItem).Value.ToString());
            Image  re=new Bitmap(img,new Size(img.Width/5,img.Height/5));
            pictureBox1.Height = re.Height;
            pictureBox1.Width =re.Width;
            pictureBox1.Image = re;
           // this.comboHinh_SelectedIndexChanged(sender, e);
        }
    }
}
